# -*- coding: UTF-8 -*-
import os, sys
from jinja2 import Template
reload(sys) 
sys.setdefaultencoding('utf8')

input_file = open('fields.txt', 'r')
lines = input_file.read().splitlines()[1:]

schema_template = open('schema_template.txt', 'r').read()
form_template = open('form_template.txt', 'r').read()
table_template = open('table_template.txt', 'r').read()


schema_output_file = open('schema.txt', 'w')
form_output_file = open('form.txt', 'w')
table_output_file = open('table.txt', 'w')

schemas_data = []
current_schema = {}
form_data = []

for line in lines:
	if line == "":
		continue
	strs = line.split('|')
	
	if len(strs) < 3:
		current_schema = {}
		current_schema["name"] = strs[0]
		current_schema["en_name"] = strs[1]
		current_schema["fields"] = []
		schemas_data.append(current_schema)
		form_data.append(current_schema)
	else:
		obj = {"name": strs[0], "description": strs[1],"type": int(strs[2])}
		current_schema["fields"].append(obj)

#print schemas_data
schema_output_file.write(Template(schema_template).render(schemas=schemas_data, module_name='invested_projects_information'))
schema_output_file.close()

form_output_file.write(Template(form_template).render(schemas=schemas_data, module_name='invested_projects_information'))
form_output_file.close()

table_output_file.write(Template(table_template).render(schemas=schemas_data, module_name='invested_projects_information'))
table_output_file.close()
